<?php

namespace Biz\model\wiki;

use Biz\service\brand\Brand;
use Doraemon\pockets\datebase\BaseDbModel;
use Medoo\Medoo;

class WikiSelectionLib extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_store';

    const _DATABASE_ = 'store';

    const _TABLE_NAME = 'wiki_selection_lib';

    const STATUS_NORMAL = 0;//默认
    const STATUS_WAIT_CHECK = 1;//待审核
    const STATUS_CHECKED = 2;//已审核
    const STATUS_DEL = 9;//已删除

    const ADM_STATUS_MAP = [
        self::STATUS_NORMAL => '待完善资料',
        self::STATUS_WAIT_CHECK => '待审核',
        self::STATUS_CHECKED => '已审核',
        self::STATUS_DEL => '已下架',
    ];

    const FREE_SHIPMENT = 1;//包邮
    const NOT_FREE_SHIPMENT = 0;//不包邮


    public static function getByWhere($where, $field = '*')
    {
        return self::get($field, $where);
    }

    public static function getInfoByitemId($itemId)
    {
        $where = [
            'itemId' => $itemId,
            'select_status[!]' => self::STATUS_DEL,
        ];
        $info = self::getByWhere($where);

        return $info;
    }

    public static function getInfosByItemIds($itemIds, $fields = '*')
    {
        $result = [];
        $where = [
            'itemId' => $itemIds,
            'select_status[!]' => self::STATUS_DEL,
        ];
        $list = self::selectByWhere($where, $fields);
        foreach ($list as $row) {
            $result[$row['id']] = $row;
        }

        return $result;
    }


    public static function selectByWhere($where, $select = '*')
    {
        $list = self::select($select, $where);

        return $list;
    }

    public static function selectCountWhere($where)
    {
        $count = self::count('id', $where);

        return $count;
    }


    public static function insertData($data)
    {
        if (!isset($data['addtime'])) {
            $data['addtime'] = date('Y-m-d H:i:s');
        }
        if (!isset($data['edittime'])) {
            $data['edittime'] = date('Y-m-d H:i:s');
        }
        $ret = self::insert($data);
        if ($ret->errorCode() !== '00000') {
            return false;
        }

        return self::id();
    }


    public static function updateData($data, $where)
    {
        if (!isset($data['edittime'])) {
            $data['edittime'] = date('Y-m-d H:i:s');
        }
        $ret = self::update($data, $where);
        if ($ret->errorCode() !== '00000') {
            return false;
        }

        return true;
    }



}